// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   CookieKeys.java

package com.hfstu.pms.util;

import com.hfstu.framework.util.StringUtil;
import com.hfstu.pms.exception.CookieNotSupportedException;
import javax.servlet.http.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CookieKeys
{

	public static final String COOKIE_SUPPORT = "cookie_support";
	public static final String V_SESSIONID = "v_sessionId";
	public static final String V_USERID = "v_userId";
	public static final String V_USERNAME = "v_userName";
	public static final String V_PASSWORD = "v_password";
	public static final String REMEMBERME = "rememberMe";
	public static final String ISSIGNEDIN = "isSignedIn";
	public static final String V_EXCEPTION = "v_exception";
	public static final int MAX_AGE = 0x1e13380;
	public static final int VERSION = 0;
	private Logger log;

	public CookieKeys()
	{
		log = LoggerFactory.getLogger(com/hfstu/pms/util/CookieKeys);
	}

	public static void addCookie(HttpServletResponse response, Cookie cookie)
	{
		String originalValue = cookie.getValue();
		cookie.setValue(originalValue);
		cookie.setVersion(0);
		response.addCookie(cookie);
	}

	public static void addSupportCookie(HttpServletResponse response)
	{
		Cookie cookieSupportCookie = new Cookie("cookie_support", "true");
		cookieSupportCookie.setPath("/");
		cookieSupportCookie.setMaxAge(0x1e13380);
		addCookie(response, cookieSupportCookie);
	}

	public static String getCookie(HttpServletRequest request, String name)
	{
		Cookie cookies[] = request.getCookies();
		if (cookies == null)
			return null;
		for (int i = 0; i < cookies.length; i++)
		{
			Cookie cookie = cookies[i];
			String cookieName = cookie.getName();
			if (cookieName.equalsIgnoreCase(name))
				return cookie.getValue();
		}

		return null;
	}

	public static boolean hasSessionId(HttpServletRequest request)
	{
		String jsessionid = getCookie(request, "v_sessionId");
		return jsessionid != null;
	}

	public static void validateSupportCookie(HttpServletRequest request)
		throws CookieNotSupportedException
	{
		String cookieSupport = getCookie(request, "cookie_support");
		if (StringUtil.isNullOrEmpty(cookieSupport))
			throw new CookieNotSupportedException();
		else
			return;
	}

	public static String getLogin(HttpServletRequest request, String paramName)
	{
		String login = request.getParameter(paramName);
		if (login == null || login.equals("null"))
			login = getCookie(request, "v_userName");
		if (login == null)
			login = "";
		return login;
	}

	public static String escape(String text)
	{
		if (text == null)
			return "";
		StringBuilder sb = new StringBuilder(text.length());
		for (int i = 0; i < text.length(); i++)
		{
			char c = text.charAt(i);
			switch (c)
			{
			case 60: // '<'
				sb.append("&lt;");
				break;

			case 62: // '>'
				sb.append("&gt;");
				break;

			case 38: // '&'
				sb.append("&amp;");
				break;

			case 34: // '"'
				sb.append("&#034;");
				break;

			case 39: // '\''
				sb.append("&#039;");
				break;

			case 40: // '('
				sb.append("&#040;");
				break;

			case 41: // ')'
				sb.append("&#041;");
				break;

			case 35: // '#'
				sb.append("&#035;");
				break;

			case 37: // '%'
				sb.append("&#037;");
				break;

			case 59: // ';'
				sb.append("&#059;");
				break;

			case 43: // '+'
				sb.append("&#043;");
				break;

			case 45: // '-'
				sb.append("&#045;");
				break;

			case 36: // '$'
			case 42: // '*'
			case 44: // ','
			case 46: // '.'
			case 47: // '/'
			case 48: // '0'
			case 49: // '1'
			case 50: // '2'
			case 51: // '3'
			case 52: // '4'
			case 53: // '5'
			case 54: // '6'
			case 55: // '7'
			case 56: // '8'
			case 57: // '9'
			case 58: // ':'
			case 61: // '='
			default:
				sb.append(c);
				break;
			}
		}

		return sb.toString();
	}
}
